//
//  EpointPhoneViewController.m
//  EpointFrame5
//
//  Created by 李亚春 on 13-12-5.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "EpointPhoneViewController.h"
#import "UIImage+Tint.h"



@interface EpointPhoneViewController ()

@end

@implementation EpointPhoneViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self initThemeColor];
    NSLog(@"UIScreenWidth=%f",UIScreenWidth);
    NSLog(@"UIScreenHeight=%f",UIScreenHeight);

    [self addCustomeBackButtonItemWithSelector:@selector(popBack)];
    self.customBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, AppWidth, AppHeight)];
    [self.customBackground setContentMode:UIViewContentModeScaleAspectFill];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview: self.customBackground];
    [self.view sendSubviewToBack: self.customBackground];
    
    
    //设置标题
    [self setTitle:[self.passParam objectForKey:@"title"]];
    
}


-(void)initThemeColor
{
    self.themeColorStr = [[NSArray alloc]initWithObjects:@"浪漫紫", nil];
//    self.themeColorArr = [NSArray arrayWithObjects:COLOR(91, 68, 106), nil];//紫色
    self.themeColorArr = [NSArray arrayWithObjects:COLOR(25, 139, 225), nil];  //蓝色
}

-(void)viewWillAppear:(BOOL)animated
{
    
    //设置主题
    NSString *themeIndex = [DBFrameService getConfigValue:Epoint_User_ThemeColor];
    if ([themeIndex isEqualToString:@""]) {
        [DBFrameService setConfigValue:Epoint_User_ThemeColor value:@"0"];
        themeIndex = @"0";
    }
    
    [self.navigationController.navigationBar setBackgroundImage:[EpointUIUtils getImageByColor:[self.themeColorArr objectAtIndex:themeIndex.intValue]] forBarMetrics:UIBarMetricsDefault];
    
    if (themeIndex.intValue>=0) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                         [UIColor whiteColor], UITextAttributeTextColor,
                                                                         nil]];
    }
    else
    {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        [self.navigationController.navigationBar setTintColor:COLOR(30, 144, 255)];
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                         [UIColor blackColor], UITextAttributeTextColor,
                                                                         nil]];
    }
    
}

-(void)addCustomeBackButtonItemWithSelector:(SEL)action
{
    UIImage *img =[[UIImage imageNamed:@"ios7_44"]imageWithTintColor:[UIColor whiteColor]];
    if (ISIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (CurrentDeviceISiPad) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStyleBordered target:self action:action];
        }else{
            UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        	temporaryBarButtonItem.title = @"返回";
            self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        }
    }
    else{
        self.navigationItem.backBarButtonItem=nil;
        UIButton* backButton= [[UIButton alloc] initWithFrame:CGRectMake(0,0,44,44)];
        [backButton setImage:img forState:UIControlStateNormal];
        [backButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    }
    if (CurrentDeviceISiPad&&!XAppDelegate.isFullScreen) {
        self.navigationItem.hidesBackButton=YES;
        self.navigationItem.leftBarButtonItem = nil;
    }else if(CurrentDeviceISiPad&&XAppDelegate.isFullScreen){
        self.navigationItem.hidesBackButton=NO;
    }
}

-(void)setTitle:(NSString *)title
{
    [super setTitle:title];
    if (!ISIOS7)
    {
        [self.titleButton setTitle:title forState:UIControlStateNormal];
    }
}

-(void)popBack
{
    //ipad exit fullscreen
    if (CurrentDeviceISiPad&&XAppDelegate.isFullScreen&&self.navigationController.viewControllers.count==1){
        XAppDelegate.leftWidth=PAD_LEFT_NAV_WIDTH_CONST;
        XAppDelegate.isFullScreen=NO;
        [XAppDelegate.stackController setLargeLeftInset:PAD_LEFT_NAV_WIDTH];
        [XAppDelegate.stackController setLeftInset:PAD_LEFT_NAV_WIDTH];
        [XAppDelegate.stackController setStackWidth:AppWidth-PAD_LEFT_NAV_WIDTH];
        
        [XAppDelegate.stackController popViewControllerAnimated:YES];
        [XAppDelegate.stackController viewWillAppear:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)validateXML:(NSObject *)obj
{
    if(obj!=nil){
        NSString *bs = (NSString *)obj;
        NSString *Status = [EpointStringUtil getXMLFormatedAttribute:bs :@"Status"];
        if([Status isEqualToString:@"True"])
        {
            return YES;
        }
        else if([Status isEqualToString:@"False"])
        {
            //String Description = StringHelp.getXMLAtt(bs, "Description");
            [self toastMessage:@"后台服务异常"];
            return NO;
        }
        else
        {
            [self toastMessage:@"网络连接超时"];
            return NO;
        }
    }
    else
    {
        [self toastMessage:@"网络连接超时"];
        return NO;
    }
}



-(CGRect)getFullContentFrame
{
    
    return CGRectMake(0, 0, AppWidth, UIViewRemain);
}

-(void)setRightBarButtonItemWithTitle:(NSString *)title action:(SEL)action
{
    if (ISIOS7) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    }
    else
    {
        CGFloat bw = 44;
        if (title.length>=4) {
            bw = 66;
        }
        UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, bw, 44)];
        [rightButton setTitle:title forState:UIControlStateNormal];
        [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [rightButton.titleLabel  setFont:[UIFont systemFontOfSize:16]];
        //        [rightButton setTitleColor:[self getThemeColor] forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    }
}

-(void)setRightBarButtonItemWithImage:(NSString *)image action:(SEL)action
{
    if (ISIOS7) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:image] style:UIBarButtonItemStyleBordered target:self action:action];
    }
    else
    {
        UIImage *img =[[UIImage imageNamed:image] imageWithTintColor:[UIColor whiteColor]];
          img = [self scaleToSize:img size:CGSizeMake(30, 30)];
        UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
        [rightButton setImage:img forState:UIControlStateNormal];
        [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    }
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}

@end
